package cn.swanit.common.base;

import cn.swanit.common.base.enums.IotCloudEnum;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result implements Serializable {

    private static final long serialVersionUID = -3243475556181265119L;

    private String code;

    private String msg;

    private transient Object data;

    public static Result ok() {
        return new Result(IotCloudEnum.OK.getCode(), IotCloudEnum.OK.getMessage(), null);
    }

    public static Result ok(String msg) {
        return new Result(IotCloudEnum.OK.getCode(), msg, null);
    }

    public static Result ok(Object data) {
        return new Result(IotCloudEnum.OK.getCode(), IotCloudEnum.OK.getMessage(), data);
    }

    public static Result ok(String msg, Object data) {
        return new Result(IotCloudEnum.OK.getCode(), msg, data);
    }

    public static Result ok(Integer currentPage, Long totalCount, List<?> tableData) {
        JSONObject json = new JSONObject();
        json.put("currentPage", currentPage);
        json.put("totalCount", totalCount);
        json.put("tableData", tableData);
        return new Result(IotCloudEnum.OK.getCode(), IotCloudEnum.OK.getMessage(), json);
    }

    public static Result error() {
        return new Result(IotCloudEnum.SYS_2000001.getCode(), IotCloudEnum.SYS_2000001.getMessage(), null);
    }

    public static Result error(IotCloudEnum iotCloudEnum) {
        return new Result(iotCloudEnum.getCode(), iotCloudEnum.getMessage(), null);
    }

    public static Result error(IotCloudEnum iotCloudEnum, Object data) {
        return new Result(iotCloudEnum.getCode(), iotCloudEnum.getMessage(), data);
    }

    public static Result error(Object data) {
        return new Result(IotCloudEnum.SYS_2000001.getCode(), IotCloudEnum.SYS_2000001.getMessage(), data);
    }

    public static Result error(String msg) {
        return new Result(IotCloudEnum.SYS_2000001.getCode(), msg, null);
    }

    public static Result error(String msg, Object data) {
        return new Result(IotCloudEnum.SYS_2000001.getCode(), msg, data);
    }
}
